package com.gs.SynAntiWandering.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.InputType;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gs.SynAntiWandering.R;

public class LabelEdit extends LinearLayout {
    private TextView item_lbl;
    private EditText item_edit;
    private String valId;

    public String getLbl() {
        return item_lbl.getText().toString();
    }

    public void setLbl(String text) {
        item_lbl.setText(text);
    }

    public void setLblHint(String hint) {
        item_lbl.setHint(hint);
    }

    public String getText() {
        return item_edit.getText().toString();
    }

    public void setText(String text) {
        if (text != null) {
            item_edit.setText(text);
        } else item_edit.setText("");
    }

    public void setTextHint(String hint) {
        if (hint != null) {
            item_edit.setHint(hint);
        } else item_edit.setHint("");
    }

    public String getValId() {
        return valId;
    }

    public void setValId(String valId) {
        this.valId = valId;
    }

    public void setInputType(int inputType){
        item_edit.setInputType(inputType);
    }

    public LabelEdit(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.LabelEdit);
        LayoutInflater.from(context).inflate(R.layout.label_edit, this, true);
        item_lbl = findViewById(R.id.item_lbl);
        item_edit = findViewById(R.id.item_edit);
        String text = attributes.getString(R.styleable.LabelEdit_label_edit_text);
        String textHint = attributes.getString(R.styleable.LabelEdit_label_edit_text_hint);
        boolean isPassword = attributes.getBoolean(R.styleable.LabelEdit_label_edit_password,false);
        boolean isNumber = attributes.getBoolean(R.styleable.LabelEdit_label_edit_number,false);
        boolean isStar = attributes.getBoolean(R.styleable.LabelEdit_label_edit_star,false);
        item_edit.setText(text);
        item_edit.setHint(textHint);
        if (isNumber) {
            item_edit.setInputType(InputType.TYPE_CLASS_NUMBER);
//            item_edit.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }
        if (isPassword) {
            item_edit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD| InputType.TYPE_CLASS_TEXT);
            item_edit.setTypeface(Typeface.DEFAULT);
        }
        String textVisibility = attributes.getString(R.styleable.LabelEdit_label_edit_visibility);
        if (textVisibility != null && !TextUtils.isEmpty(textVisibility)) {
            switch (textVisibility) {
                case "visible":
                    item_edit.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_edit.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_edit.setVisibility(GONE);
                    break;
            }
        }
        String lbl = attributes.getString(R.styleable.LabelEdit_label_edit_lbl);
        if (isStar) {
            Spannable spannable = new SpannableString("* "+lbl);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.red)),0,1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            item_lbl.setText(spannable);
        }else {
            item_lbl.setText(lbl);
        }
        String lblVisibility = attributes.getString(R.styleable.LabelEdit_label_edit_lbl_visibility);
        if (lblVisibility != null && !TextUtils.isEmpty(lblVisibility)) {
            switch (lblVisibility) {
                case "visible":
                    item_lbl.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_lbl.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_lbl.setVisibility(GONE);
                    break;
            }
        }
        attributes.recycle();
    }
}
